package com.tutu.hosp.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tutu.common.result.Result;
import com.tutu.common.utils.MD5;
import com.tutu.model.hosp.HospitalSet;
import com.tutu.hosp.service.HospitalSetService;
import com.tutu.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @Program: yygh_parent
 * @Description:
 * @Author: JK.TuTu
 * @Create: 2022-09-16 22:13
 **/
@Slf4j
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 测试查找所有医院信息
     *
     * @return
     */
    @ApiOperation("查找所有医院信息")
    @GetMapping("findAll")
    public Result findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        if (CollectionUtil.isNotEmpty(list)) {
            return Result.ok(list);
        } else {
            return Result.fail();
        }

    }

    /**
     * 逻辑删除医院
     *
     * @param id 医院id
     * @return
     */
    @ApiOperation("逻辑删除所有医院信息")
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Long id) {
        log.info("逻辑删除的医院id:{}", id);
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 分页查询医院对象
     *
     * @param current            当前页数
     * @param limit              每页记录数
     * @param hospitalSetQueryVo
     * @return
     */
    @ApiOperation("分页查询医院对象")
    @PostMapping("selectForPage/{current}/{limit}")
    public Result selectForPage(@PathVariable("current") long current, @PathVariable("limit") long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        log.info("查询的实体对象:{}", hospitalSetQueryVo);
        Page<HospitalSet> page = new Page<>(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
        if (!StrUtil.isEmpty(hosname)) {
            wrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StrUtil.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }
        //调用方法实现分页查询
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        //返回结果
        return Result.ok(pageHospitalSet);
    }

    /**
     * 添加医院信息
     *
     * @param hospitalSet
     * @return
     */
    @ApiOperation("添加医院信息")
    @PostMapping("saveHospitalSet")
    public Result save(@RequestBody HospitalSet hospitalSet) {
        log.info("添加的医院信息:{}", hospitalSet);
        //设置状态
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     *
     * @param id
     * @return
     */
    /**
     * 根据id查找医院
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查找医院")
    @GetMapping("getHospitalById/{id}")
    public Result getHospitalById(@PathVariable("id") Long id) {
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }

    /**
     * 修改医院信息
     *
     * @param hospitalSet
     * @return
     */
    @ApiOperation("修改医院信息")
    @PostMapping("updateHospital")
    public Result updateHospital(@RequestBody HospitalSet hospitalSet) {
        log.info("修改的医院信息:{}", hospitalSet);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 批量删除医院信息
     *
     * @param idList
     * @return
     */
    @ApiOperation("批量删除医院信息")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        log.info("批量删除的医院id:{}", idList);
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 修改医院信息  锁定和取消锁定
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation("根据id修改医院状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Long id, @PathVariable("status") int status) {
        log.info("修改医院的id:{}", id);
        LambdaUpdateWrapper<HospitalSet> updateWrapper = new LambdaUpdateWrapper<>();
        //添加更新条件
        updateWrapper.eq(HospitalSet::getId, id).set(HospitalSet::getStatus, status);
        hospitalSetService.update(updateWrapper);
        return Result.ok();
    }


}

